package com.ruoyi.Material;

import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
@RequestMapping("/bascPdf")
public class PdfController {
    // 正则表达式与之前保持一致
    private final YuchaiPDFService pdfService;

    public PdfController(YuchaiPDFService pdfService) {
        this.pdfService = pdfService;
    }

    /**
     * 接收前端传入的《玉柴855PDF汇总》文件并处理
     */
    @PostMapping("/process-yuchai-pdf")
    public String processPDF(@RequestParam("file") MultipartFile file) {
        try {
            // 仅处理指定文件名的文件
            if (!"玉柴855PDF汇总 李博 2025.07.04.pdf".equals(file.getOriginalFilename())) {
                return "仅支持处理《玉柴855PDF汇总 李博 2025.07.04.pdf》文件";
            }
            // 处理文件并返回结果
            String result = pdfService.process(file);
            return result;
        } catch (IOException e) {
            return "处理失败：" + e.getMessage();
        }
    }
}
